In [1]:
X = np.array([[1,0,0],[1,1,1], [0,1,1],[0,1,0],[0,0,1],[1,1,1]])
y0 = np.zeros(2)
y1 = np.ones(4)
y = np.hstack([y0, y1])
print(X)
print(y)
In [2]:
from sklearn.naive_bayes import BernoulliNB
clf_bern = BernoulliNB().fit(X, y)
In [3]:
clf_bern.classes_
Out[3]:
In [4]:
clf_bern.class_count_
Out[4]:
In [5]:
fc = clf_bern.feature_count_
fc
Out[5]:
In [6]:
fc / np.repeat(clf_bern.class_count_[:, np.newaxis], 3, axis=1)
Out[6]:
In [7]:
x_new = np.array([1,1,0])
In [8]:
clf_bern.predict_proba([x_new])
Out[8]:
In [24]:
np.exp(clf_bern.feature_log_prob_)
Out[24]:
In [25]:
theta = np.exp(clf_bern.feature_log_prob_) #자동적으로 스무딩
theta
Out[25]:
In [27]:
p = ((theta**x_new)*(1-theta)**(1-x_new)).prod(axis=1)*np.exp(clf_bern.class_log_prior_)
p / p.sum()
Out[27]:
In [28]:
X1 = np.array([[1,0,0],[1,0,1], [0,1,1],[0,1,0],[0,0,1],[1,1,1]])
y01 = np.zeros(2)
y11 = np.ones(4)
y1 = np.hstack([y01, y11])
In [29]:
clf_bern1 = BernoulliNB().fit(X1, y1)
In [30]:
fc1 = clf_bern1.feature_count_
fc1
Out[30]:
In [31]:
np.repeat(clf_bern1.class_count_[:, np.newaxis], 3, axis=1)
Out[31]:
In [32]:
fc1 / np.repeat(clf_bern1.class_count_[:, np.newaxis], 3, axis=1)
Out[32]:
In [33]:
clf_bern1.predict_proba([x_new])
Out[33]:
In [35]:
np.exp(clf_bern1.feature_log_prob_)
Out[35]:
In [36]:
theta = np.exp(clf_bern1.feature_log_prob_)
theta
Out[36]:
In [37]:
p = ((theta**x_new)*(1-theta)**(1-x_new)).prod(axis=1)*np.exp(clf_bern1.class_log_prior_)
p / p.sum()
Out[37]:
In [38]:
X = np.array([[4,4,2],[4,3,3], [6,3,1],[4,6,0],[0,4,1],[1,3,1],[1,1,3],[0,3,2]])
y0 = np.zeros(4)
y1 = np.ones(4)
y = np.hstack([y0, y1])
print(X)
print(y)
In [39]:
from sklearn.naive_bayes import MultinomialNB
clf_mult = MultinomialNB().fit(X, y)
In [40]:
clf_mult.classes_
Out[40]:
In [41]:
clf_mult.class_count_
Out[41]:
In [42]:
fc = clf_mult.feature_count_
fc
Out[42]:
In [43]:
np.repeat(fc.sum(axis=1)[:, np.newaxis], 3, axis=1)
Out[43]:
In [44]:
fc / np.repeat(fc.sum(axis=1)[:, np.newaxis], 3, axis=1)
Out[44]:
In [45]:
clf_mult.alpha
Out[45]:
In [46]:
(fc + clf_mult.alpha) / (np.repeat(fc.sum(axis=1)[:, np.newaxis], 3, axis=1) + clf_mult.alpha * X.shape[1])
Out[46]:
In [47]:
np.repeat(fc.sum(axis=1)[:, np.newaxis], 3, axis=1) + clf_mult.alpha * X.shape[1]
Out[47]:
In [48]:
x_new1 = np.array([1,1,1])
clf_mult.predict_proba([x_new1])
Out[48]:
In [49]:
x_new2 = np.array([2,2,2])
clf_mult.predict_proba([x_new2])
Out[49]:
In [50]:
x_new3 = np.array([3,3,3])
clf_mult.predict_proba([x_new3])
Out[50]:
In [ ]:
In [51]:
X = np.array([
[1, 0, 0],
[1, 0, 1],
[0, 0, 1],
[0, 0, 0],
[1, 1, 1],
[0, 1, 1],
[0, 0, 1],
[0, 1, 0],
])
y = np.array([0,0,0,0,1,1,1,1])
(1) 사전 분포(prior) p(y)를 구하세요.
In [52]:
py0, py1 = (y==0).sum()/len(y), (y==1).sum()/len(y)
py0, py1
Out[52]:
(2) 스무딩 벡터 알파=0 일 때, 다음 x_new에 대해 우도(likelihood)함수 p(x|y)를 구하고 조건부 확률 분포 p(y|x)를 구하세요.(normalize 된 값이 아님!)
In [56]:
x_new = np.array([1, 1, 0])
In [58]:
theta0 = X[y==0, :].sum(axis=0)/len(X[y==0, :])
theta0
Out[58]:
In [59]:
theta1 = X[y==1, :].sum(axis=0)/len(X[y==1, :])
theta1
Out[59]:
In [60]:
likelihood0 = (theta0**x_new).prod()*((1-theta0)**(1-x_new)).prod()
likelihood0
Out[60]:
In [61]:
likelihood1 = (theta1**x_new).prod()*((1-theta1)**(1-x_new)).prod()
likelihood1
Out[61]:
In [62]:
px = likelihood0 * py0 + likelihood1 * py1
px
Out[62]:
In [63]:
likelihood0 * py0 / px, likelihood1 * py1 / px
Out[63]:
In [64]:
from sklearn.naive_bayes import BernoulliNB
model = BernoulliNB(alpha=0).fit(X, y)
model.predict_proba([x_new])
Out[64]:
(3) 스무딩 팩터 알파=0.5일 때, 문제(2)를 다시 풀어보세요.
In [67]:
theta0 = (X[y==0, :].sum(axis=0) + 0.5*np.ones(3))/(len(X[y==0,:])+1)
theta0
Out[67]:
In [68]:
theta1 = (X[y==1, :].sum(axis=0) + 0.5*np.ones(3))/(len(X[y==1,:])+1)
theta1
Out[68]:
In [69]:
x_new = np.array([1, 1, 0])
In [70]:
likelihood0 = (theta0**x_new).prod()*((1-theta0)**(1-x_new)).prod()
likelihood0
Out[70]:
In [71]:
likelihood1 = (theta1**x_new).prod()*((1-theta1)**(1-x_new)).prod()
likelihood1
Out[71]:
In [72]:
px = likelihood0 * py0 + likelihood1 * py1
px
Out[72]:
In [73]:
likelihood0 * py0 / px, likelihood1 * py1 / px
Out[73]:
In [75]:
from sklearn.naive_bayes import BernoulliNB
model = BernoulliNB(alpha=0.5).fit(X, y)
model.predict_proba([x_new])
Out[75]:
(1) 사전 분포(prior) p(y)를 구하세요.
(2) 스무딩 팩터 알파=0 일 때, 다음 x_new에 대해 우도(likelihood)함수 p(x|y)를 구하고 조건부 확률 분포 p(y|x)를 구하세요.(normalize 된 값이 아님!)
In [76]:
x_new = np.array([2, 3, 1])
In [77]:
theta0 = X[y==0, :].sum(axis=0)/X[y==0, :].sum()
theta0
Out[77]:
In [78]:
theta1 = X[y==1, :].sum(axis=0)/X[y==1, :].sum()
theta1
Out[78]:
In [79]:
likelihood0 = (theta0**x_new).prod()
likelihood0
Out[79]:
In [80]:
likelihood1 = (theta1**x_new).prod()
likelihood1
Out[80]:
In [81]:
px = likelihood0 * py0 + likelihood1 * py1
px
Out[81]:
In [82]:
likelihood0 * py0 / px, likelihood1 * py1 / px
Out[82]:
In [83]:
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB(alpha=0).fit(X, y)
model.predict_proba([x_new])
Out[83]:
(3) 스무딩 팩터 알파=0.5일 때, 문제(2)를 다시 풀어보세요.
In [84]:
theta0 = (X[y==0, :].sum(axis=0) + 0.5*np.ones(3))/ (X[y==0, :].sum() + 1.5)
theta0
Out[84]:
In [85]:
theta1 = (X[y==1, :].sum(axis=0) + 0.5*np.ones(3))/ (X[y==1, :].sum() + 1.5)
theta1
Out[85]:
In [86]:
likelihood0 = (theta0**x_new).prod()
likelihood0
Out[86]:
In [87]:
likelihood1 = (theta1**x_new).prod()
likelihood1
Out[87]:
In [88]:
px = likelihood0 * py0 + likelihood1 * py1
px
Out[88]:
In [89]:
likelihood0 * py0 / px, likelihood1 * py1 / px
Out[89]:
In [90]:
from sklearn.naive_bayes import MultinomialNB
model = MultinomialNB(alpha=0.5).fit(X, y)
model.predict_proba([x_new])
Out[90]: